<?php

namespace backend\modules\supplier\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\supplier\models\Slot;

/**
 * SlotSearch represents the model behind the search form about `backend\modules\supplier\models\Slot`.
 */
class SlotSearch extends Slot
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'supplier_id', 'storage_id', 'rack_id', 'sort', 'length', 'width', 'height'], 'integer'],
            [['name'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $params['slotSearch']['supplier_id'] = Yii::$app->user->identity->supplier_id;
        $query = Slot::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'supplier_id' => $this->supplier_id,
            'storage_id' => $this->storage_id,
            'rack_id' => $this->rack_id,
            'sort' => $this->sort,
            'length' => $this->length,
            'width' => $this->width,
            'height' => $this->height,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name]);

        return $dataProvider;
    }
}
